package com.student.liu.controller;

import com.alibaba.fastjson.JSONObject;
import com.student.liu.entity.User;
import com.student.liu.service.IUserService;
import com.student.liu.utils.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.xml.bind.ValidationException;
import java.util.Date;
import java.util.Map;

@Slf4j
@RestController
@Api(tags = "登录相关接口")
public class IndexController extends BaseController {

    @PostMapping("test")
    public Object test(@RequestParam String param) {
        return success("hello world,这是我第一个SpringBoot项目，我接收到一个参数是：" + param, null);
    }

    @Resource
    IUserService userService;

    @Data
    static class LoginParam{
        @NotBlank(message = "用户名不能为空")
        private String username;
        @NotBlank(message = "密码不能为空")
        private String password;
    }


    /**
     * 登录
     * @param param
     * @return
     */
    @PostMapping("login")
    @ApiOperation("登录验证")
    public Object login(@Valid @RequestBody LoginParam param, BindingResult result) throws ValidationException {
        User user = new User(param.getUsername(), param.getPassword());
        Map<String, Object> map = userService.login(user);
        Date issuedTime = new Date();
        Date expiresTime = new Date(issuedTime.getTime() + 1000 * 60 * 60 * 72);  //过期时间为72个小时
        map.put("token", TokenUtils.generate(map.get("id"), issuedTime, expiresTime));
        return success("登录成功", map);
    }

}
